package com.zxa.controller;

import com.zxa.dao.ProductImpl;
import com.zxa.pojo.Product;
import com.zxa.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * Created by shixi006 on 2017/7/21.
 */
@Controller
public class TestController {


    @Autowired
    ProductImpl proDao;

    @RequestMapping("/test.do")
    public String getAll(ModelMap map){
        System.out.println("测试");



        map.addAttribute("all", proDao.getAll());
        return "proList";
    }

    @RequestMapping("/saveAll.do")
    public String getSaveAll(ModelMap map){
        System.out.println("测试");
        proDao.getAllFromDB();
        map.addAttribute("all",proDao.getAll());
        return "proList";
    }


    @RequestMapping("/update.do")
    public String update(ModelMap map,String id){
        map.addAttribute("pro",proDao.get(id));
        return "addOrUpdate";
    }

    @RequestMapping("/add.do")
    public String add(ModelMap map,String id){
        return "addOrUpdate";
    }
    @RequestMapping("/delete.do")
    public String delete(ModelMap map,String id){
        proDao.delete(id);
        return getAll(map);
    }

    @RequestMapping("/deleteAll.do")
    public String deleteAll(ModelMap map,String id){
        proDao.deleteAll();
        return getAll(map);
    }

    @RequestMapping(value = "/addOrUpdate.do",method = RequestMethod.POST)
    public String addOrUpdate(ModelMap map,String id,Product product,String create){
        System.out.println(product.getProName()+id);
        try {
            product.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").parse(create));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(product.getCreateTime());
        proDao.addOrUpdate(product);
        return getAll(map);
    }

}
